package xuegao.study.mall.order.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import xuegao.study.mall.common.dto.*;
import xuegao.study.mall.common.model.oms.OmsOrderOperateHistory;
import xuegao.study.mall.repo.dao.oms.OmsOrderDao;
import xuegao.study.mall.common.model.oms.OmsOrder;
import xuegao.study.mall.order.service.OrderService;
import xuegao.study.mall.repo.dao.oms.OmsOrderOperateHistoryDao;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OmsOrderDao omsOrderDao;

    @Autowired
    private OmsOrderOperateHistoryDao omsOrderOperateHistoryDao;

    @Override
    public int delete(List<Long> ids) {
        return omsOrderDao.deleteByIds(ids, 1);
    }

    @Override
    public int close(List<Long> ids, String note) {
        int count = omsOrderDao.updateOrderStatus(ids, 4);
        List<OmsOrderOperateHistory> historyList = ids.stream().map(orderId -> {
            OmsOrderOperateHistory history = new OmsOrderOperateHistory();
            history.setOrderId(orderId);
            history.setCreateTime(new Date());
            history.setOperateMan("后台管理员");
            history.setOrderStatus(4);
            history.setNote("订单关闭:" + note);
            return history;
        }).collect(Collectors.toList());
        omsOrderOperateHistoryDao.batchInsert(historyList);
        return count;
    }

    @Override
    public List<OmsOrder> list(OmsOrderQueryParam queryParam, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        return omsOrderDao.getList(queryParam);
    }

    @Override
    @Cacheable(value = "0", key = "'oms:order:'+#id", unless = "#result==null")
    public OmsOrderDetail detail(Long id) {
        return omsOrderDao.getDetail(id);
    }

    @Override
    public int updateMoneyInfo(OmsMoneyInfoParam moneyInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(moneyInfoParam.getOrderId());
        order.setFreightAmount(moneyInfoParam.getFreightAmount());
        order.setDiscountAmount(moneyInfoParam.getDiscountAmount());
        order.setModifyTime(new Date());
        // 更新
        int count = omsOrderDao.updateByPrimaryKey(order);
        // 插入记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(moneyInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(moneyInfoParam.getStatus());
        history.setNote("修改费用信息");
        omsOrderOperateHistoryDao.insert(history);
        return count;
    }

    @Override
    public int updateReceiverInfo(OmsReceiverInfoParam receiverInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(receiverInfoParam.getOrderId());
        order.setReceiverName(receiverInfoParam.getReceiverName());
        order.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        order.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        order.setReceiverProvince(receiverInfoParam.getReceiverProvince());
        order.setReceiverCity(receiverInfoParam.getReceiverCity());
        order.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        order.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        order.setModifyTime(new Date());
        // 更新
        int count = omsOrderDao.updateByPrimaryKey(order);
        // 插入记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(receiverInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(receiverInfoParam.getStatus());
        history.setNote("修改收货人信息");
        omsOrderOperateHistoryDao.insert(history);
        return count;
    }

    @Override
    public int updateNote(Long id, String note, Integer status) {
        OmsOrder order = new OmsOrder();
        order.setId(id);
        order.setNote(note);
        order.setModifyTime(new Date());
        // 更新
        int count = omsOrderDao.updateByPrimaryKey(order);
        // 插入记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(id);
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(status);
        history.setNote("修改备注信息");
        omsOrderOperateHistoryDao.insert(history);
        return count;
    }

    @Override
    public int delivery(List<OmsOrderDeliveryParam> deliveryParamList) {
        // 批量发货
        int count = omsOrderDao.delivery(deliveryParamList);
        // 添加操作记录
        List<OmsOrderOperateHistory> historyList = deliveryParamList.stream().map(omsOrderDeliveryParam -> {
            OmsOrderOperateHistory history = new OmsOrderOperateHistory();
            history.setOrderId(omsOrderDeliveryParam.getOrderId());
            history.setCreateTime(new Date());
            history.setOperateMan("后台管理员");
            history.setOrderStatus(2);
            history.setNote("完成发货");
            return history;
        }).collect(Collectors.toList());
        omsOrderOperateHistoryDao.batchInsert(historyList);
        return count;
    }
}
